// pages/cart/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        num:0,
        check:wx.getStorageSync('check') || false,
        cartList:[],
        adress:{
            name:'张三',
            adress:'广东省广州市海珠区新港中路397号',
            mobile:'020-81167888'
        }
    },
    // 进入订单
    onClickButton(){
        // console.log(this.data.cartList);
        let arr = this.data.cartList.filter(item=>{
            return item.check;
        })
        console.log(arr);
        wx.setStorageSync('orderList', arr);
        wx.setStorageSync('adress',this.data.adress);
        wx.navigateTo({
          url: '/pages/order_list/index?num='+this.data.num,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        
    },
    checked(){
        var check = !this.data.check;
        var cartList = this.data.cartList.map((item) => {
            item.check = check;
            return item;
        })
        this.setData({
            check:check,
            cartList:cartList
        })
        wx.setStorageSync('check',check);
        wx.setStorageSync('cartList',cartList);
        this.getPrice();
    },
    checkItem(e){
        console.log(e,35);
        var index = e.target.dataset.index;
        this.data.cartList[index].check = !this.data.cartList[index].check;
        wx.setStorageSync('cartList',this.data.cartList);
        // console.log(21);
        this.setData({
            cartList:wx.getStorageSync('cartList')
        })
        var num = 0;
        this.data.cartList.forEach(item=>{
            if(item.check){
                num+=1;
            }
        })
        if(this.data.cartList.length != num){
            this.setData({
                check:false
            })
        }else {
            this.setData({
                check:true
            })
        }
        wx.setStorageSync('check',this.data.check);
        this.getPrice();
    },
    getPrice(){
        let num = 0;
        this.data.cartList.forEach(item=>{
            if(item.check){
                num += item.num*item.goods_price;
            }
        })
        num = Number(num+'00');
        this.setData({
            num:num,
        })
    },
    changeNum(e){
        console.log(e);
        var index = e.target.dataset.index;
        this.data.cartList[index].num = e.detail;
        wx.setStorageSync('cartList', this.data.cartList);
        this.setData({
            cartList:wx.getStorageSync('cartList')
        })
        this.getPrice();
    },
    // 跳转到地址页
    goAdress(){
        wx.redirectTo({
          url: '/pages/adress/index',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        let cart = wx.getStorageSync('cartList') || []
        this.setData({
            cartList:cart
        })
        this.getPrice();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})